from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
import datetime
import requests
import re


class DataBook(object):
    browser  = webdriver.Chrome(
        executable_path='..\\chromedriver.exe')
    url =""

    def initBook(self,url,data):
        self.url = url
        self.browser.get(url)
        time.sleep(2)
        index = data[4];
        if index<26:
            print("越城区")##点击越城区
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[1]/a').click()
        elif index <41:
            print("柯桥区")  ##点击柯桥区
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[2]/a').click()
        elif index <49:
            print("上虞区")  ##点击上虞区
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[3]/a').click()
        elif index <53:
            print("诸暨市")  ##点击诸暨市
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[4]/a').click()
        elif index <57:
            print("嵊州区")  ##点击嵊州区
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[5]/a').click()
        else:
            print("新昌区")  ##点击新昌区
            self.browser.find_element_by_xpath('//*[@id="recent-tab"]/li[6]/a').click()

        time.sleep(1)
        
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[1]/div[1]/input').clear()##收件人姓名
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[1]/div[1]/input').send_keys(data[0])

        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[1]/div[2]/input').clear()##收件人身份证号
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[1]/div[2]/input').send_keys(data[1])

        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[2]/div[1]/input').clear()  ##收件人联系电话
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[2]/div[1]/input').send_keys(data[2])

        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[2]/div[2]/input').clear()  ##收件人医保卡号(9位)
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[2]/div[2]/input').send_keys(data[3])


        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[4]/div/div/input').clear()  ##验证码
        self.browser.find_element_by_xpath('//*[@id="main-container"]/div/div/div/div/div/div[1]/div/div[2]/div[2]/div[1]/form/div[4]/div/div/input').send_keys(input(url + '请输入验证码：'))  ##输入验证码
        time.sleep(1)
        self.seconds = self.getTime(url)#获取服务器时间


    def doBook(self):
        try:
            self.browser.find_element_by_class_name('btn-success').click()
            print(self.url+"已经成功请求申购！")
        except:
            print("发生异常")

    def getTime(self,url):
        try:
            # 设置头信息，没有访问会错误400！！！
            headers = {'User-Agent': 'Mozilla/5.0'}
            # 用requests get这个地址，带头信息的；
            r = requests.get(url=url, headers=headers)
            ts = r.headers["Date"]
            ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
            ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)
            dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
            tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
            d1 = datetime.datetime(ttime.tm_year, ttime.tm_mon, ttime.tm_mday, ttime.tm_hour, ttime.tm_min,ttime.tm_sec)
            d2 = datetime.datetime(ttime.tm_year, ttime.tm_mon, ttime.tm_mday,15, 00, 00)
            print(dat, tm)
            return (d2 - d1).seconds
        except:
            return -1


    def getPort(self):
        try:
            url = "http://health.sxws.gov.cn/";
            # 设置头信息，没有访问会错误400！！！
            headers = {'User-Agent': 'Mozilla/5.0'}
            # 用requests get这个地址，带头信息的；
            r = requests.get(url=url, headers=headers)
            p = re.compile(r'([0-9]{1,}.){4}:[0-9]{1,}')
            text = p.search(r.text)[0]
            if text != "":
                port = text.split(':')[1]
                if port == '':
                    return input("获取失败，请手动输入端口：")
                return port
        except:
            return input("获取失败，请手动输入端口：")

    def getData(self,index):
        data =[
            ["姓名", "身份证", "电话", "医保卡", 47],  #第1个链接
            ["姓名", "身份证", "电话", "医保卡", 47],  #第2个链接
            ["姓名", "身份证", "电话", "医保卡", 47],  #第3个链接
            ["姓名", "身份证", "电话", "医保卡", 47]], #第4个链接
            ["姓名", "身份证", "电话", "医保卡", 47],  #第5个链接
            ["姓名", "身份证", "电话", "医保卡", 47]   #第6个链接
        ]

        return data[index-1]
